package com.ruoyi.web.controller.wq;

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.VoteService;
import com.ruoyi.wq.domain.vo.WqVoteVO;
import com.ruoyi.wq.service.IWqVoteService;
import cn.hutool.core.util.IdUtil;

/**
 * 投票Controller
 * 
 * @author ruoyi
 * @date 2022-11-09
 */
@RestController
@RequestMapping("/wq/vote")
public class WqVoteController extends BaseController {
	
    @Autowired
    private IWqVoteService wqVoteService;
    
    @Autowired
    private VoteService voteService;
    
    @Autowired
	private RedisCache redisCache;

    /**
     * 查询投票列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(WqVoteVO wqVote){
        return wqVoteService.getPageList(wqVote);
    }
    
    /**
     * 查询投票列表
     */
    @Anonymous
    @GetMapping("/getVoteList")
    public AjaxResult getVoteList(WqVoteVO wqVote){
    	AjaxResult ajax = AjaxResult.success();
    	WqVoteVO vote = voteService.getVoteByUserId(wqVote.getUserId());
        List<WqVoteVO> dataList = voteService.getVoteList(wqVote);
        ajax.put("dataList", dataList);
        ajax.put("vote", vote);
        return ajax;
    }
    
    /**
     * 获取投票详细信息
     */
    @Anonymous
    @GetMapping(value = "/getDetail")
    public AjaxResult getDetail(String id){
        return AjaxResult.success(wqVoteService.getDetail(id));
    }

    /**
     * 新增投票
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody WqVoteVO wqVote){
    	voteService.saveVote(wqVote);
        return AjaxResult.success(wqVote);
    }
    
    /**
     * 新增投票
     */
    @PostMapping("/addVote")
    public AjaxResult addVote(@RequestBody WqVoteVO wqVote){
    	voteService.addVote(wqVote);
        return AjaxResult.success(wqVote);
    }
    
    /**
     * 获取投票编号
     */
    @Anonymous
    @GetMapping(value = "/getUid")
    public AjaxResult getUid(){
    	String uid = IdUtil.fastUUID();
    	redisCache.setCacheObject(uid, 0, 30L, TimeUnit.MINUTES);
        return AjaxResult.success(uid);
    }

}
